package com.zkq.graduate.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.tencentcloudapi.captcha.v20190722.CaptchaClient;
import com.tencentcloudapi.captcha.v20190722.models.DescribeCaptchaResultRequest;
import com.tencentcloudapi.captcha.v20190722.models.DescribeCaptchaResultResponse;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author zhangkaiqiang
 * @Date 2019/11/19  11:06
 * @Description TODO
 * 腾讯图片验证码后台验证工具类
 */
public class PhotoCaptchaUtil {


	private static final String APP_ID="2053463569";
	private static final String APP_SECRET_KEY="0X6k06mUIQoBXGPtC1zmoRg**";

	public static Map<String,Object> checkPhotoCaptcha(String ticket, String randStr, String userIp){
		Map<String,Object> resultMap=new HashMap<>();
		try{

			// 实例化一个认证对象,两个参数分别代表SecretId与SecretKey
			Credential cred = new Credential("AKIDylI75pBfnfLlwzqNiojPq7BzWnoZBORH", "ykHZcQyGJE5siYEfvwgFYdV7XozxFZhZ");

			// 实例化要发送验证码请求的client对象
			HttpProfile httpProfile = new HttpProfile();
			httpProfile.setEndpoint("captcha.tencentcloudapi.com");

			ClientProfile clientProfile = new ClientProfile();
			clientProfile.setHttpProfile(httpProfile);

			CaptchaClient client = new CaptchaClient(cred, "ap-beijing", clientProfile);

			Map<String,Object> paramMap=new HashMap<>();
			paramMap.put("CaptchaType",9);
			paramMap.put("Ticket",ticket);
			paramMap.put("UserIp",userIp);
			paramMap.put("Randstr",randStr);
			paramMap.put("CaptchaAppId",APP_ID);
			paramMap.put("AppSecretKey",APP_SECRET_KEY);
			String params= JSON.toJSONString(paramMap);
			DescribeCaptchaResultRequest req = DescribeCaptchaResultRequest.fromJsonString(params, DescribeCaptchaResultRequest.class);

			DescribeCaptchaResultResponse resp = client.DescribeCaptchaResult(req);
			// 得到返回的信息
			JSONObject result=JSON.parseObject(DescribeCaptchaResultRequest.toJsonString(resp));

			resultMap.put("code", (Integer) result.get("CaptchaCode"));
			resultMap.put("msg",result.get("CaptchaMsg"));
		} catch (TencentCloudSDKException e) {
			System.out.println(e.toString());
		}
		return resultMap;
	}
}
